<?php


namespace app\common\cachemodel;


use app\aliyun\controller\AliyunFilterWord;
use app\common\lib\exception\ApiException;
use app\common\model\BaseCache;
use Exception;
use think\Cache;
use think\response\Json;
use XznsTools\cache\CacheManage;

/**
 * 定制报告问题记录模型
 * Class ZlcrCustomReportQuestion
 * @package app\common\cachemodel
 */
class ZlcrCustomReportQuestionInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_custom_report_question_info';
    protected $cacheModel = 'custom_report.question_info';

    static public $supplementInfoKey = 'custom_report_supplement_info:'; //custom_report_supplement_info:【uid】     定制报告扩展问题题目临时缓存 5小时

    protected $cacheIdKey = 'qid';

    /**
     * 检测补充问题
     * @param $uid
     * @param $SupplementQuestion
     * @return array[]
     * @throws Exception
     */
    public function checkSupplementQuestion($uid, $SupplementQuestion)
    {
        $isAbnormal = false;//是否异常
        //获取用户补充问题缓存
        $SupplementQuestionCache = Cache::get(self::$supplementInfoKey . $uid);
        $res = [
            'supplement_info' => [],
            'abnormal_supplement_info' => [],
        ];
        foreach ($SupplementQuestion as $key => $item)
        {
            $tmp1 = $tmp2 = [
                'title' => $SupplementQuestionCache[$key]['title'],
                'type' => $SupplementQuestionCache[$key]['type'],
            ];
            if ((int)$SupplementQuestionCache[$key]['type'] === 2)
            {
                $tmp1['content'] = $tmp2['content'] = $SupplementQuestionCache[$key]['option_list'][$item - 1]['content'];
            } else
            {
                $tmp1['content'] = $tmp2['content'] = $item;
                //检测补充问题
                $check = AliyunFilterWord::checkText($item, 'custom_supplement_question');
                if ($check)
                {
                    if ($check['filteredContent'])
                    {
                        $isAbnormal = true;
                        $tmp2['content'] = $check['filteredContent'];
                    } elseif ($check['text'])
                    {
                        $isAbnormal = true;
                        foreach ($check['text'] as $text)
                        {
                            $item = str_replace($text, '***', $item);
                        }
                        $tmp2['content'] = $item;
                    }
                }
            }
            $res['supplement_info'][] = $tmp1;
            $res['abnormal_supplement_info'][] = $tmp2;
        }
        if ($isAbnormal === false)
        {
            $res['abnormal_supplement_info'] = [];
        }
        return $res;
    }

    /**
     * 添加用户提问记录
     * @param $insertData
     * @return false|int|string
     */
    public function saveData($insertData)
    {
        try
        {
            $id = $this->insertGetId($insertData);
        }
        catch (Exception $e)
        {
            return false;
        }
        return $id;
    }

    public function updateData($data, $qid)
    {
        try
        {
            $this->where(['qid' => $qid])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $qid)->setCache($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 批量获取问题结果信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['qid' => ['in', $ids]];
        try
        {
            $data = $this->where($where)->column('rid1,rid2,answer_content,recode_info,supplement_info,abnormal_supplement_info', 'qid');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
